//
//  AddCell.m
//  植物管理
//
//  Created by liujianlong on 2022/11/21.
//

#import "AddCell.h"
@interface AddCell()
@property (strong, nonatomic) IBOutlet UILabel *titleLbl;
@property (strong, nonatomic) IBOutlet CCTextField *input;
@property (strong, nonatomic) UIButton *rightBtn;

@end
@implementation AddCell

- (void)awakeFromNib {
    [super awakeFromNib];
}
- (void)setTitle:(NSString *)title{
    _title = title;
    if (self.model == nil) { return;}
    self.titleLbl.text = LS(title);
    if ([title isEqualToString:@"植物"]) {
        self.rightBtn.hidden = YES;
        self.input.enabled = YES;
        self.input.text = self.model.plant;
        self.input.placeholder = LS(@"请输入植物名称");
    }else if ([title isEqualToString:@"数量"]){
        self.rightBtn.hidden = NO;
        self.input.enabled = YES;
        [self.rightBtn setTitle:LS(@"颗") forState:UIControlStateNormal];
        [self.rightBtn setImage:nil forState:UIControlStateNormal];
        self.input.placeholder = LS(@"请输入植物数量");
        self.input.text = self.model.num;
    }else if ([title isEqualToString:@"记农事"]){
        self.rightBtn.hidden = NO;
        self.input.enabled = NO;
        [self.rightBtn setTitle:@"" forState:UIControlStateNormal];
        [self.rightBtn setImage:[UIImage imageNamed:@"箭头-右"] forState:UIControlStateNormal];
        self.input.placeholder = LS(@"记农事");
        if (self.model.job.length > 0) {
            self.input.text = [NSString stringWithFormat:@"%@,%@%@",LS(self.model.job),self.model.times,LS(@"次")];
        }else{
            self.input.text = @"";
        }
    }else if ([title isEqualToString:@"简介信息"]){
        self.rightBtn.hidden = NO;
        self.input.enabled = NO;
        [self.rightBtn setTitle:@"" forState:UIControlStateNormal];
        [self.rightBtn setImage:[UIImage imageNamed:@"箭头-右"] forState:UIControlStateNormal];
        self.input.placeholder = LS(@"植物简介");
        self.input.text = self.model.plantDetail;
    }else if ([title isEqualToString:@"种植时间"]){
        self.rightBtn.hidden = NO;
        self.input.enabled = NO;
        [self.rightBtn setTitle:@"" forState:UIControlStateNormal];
        [self.rightBtn setImage:[UIImage imageNamed:@"下拉"] forState:UIControlStateNormal];
        self.input.placeholder = @"";
        self.input.text = [NSDate transToDate:@"yyyy-MM-dd" :self.model.stamp];
    }
}
- (void)setModel:(Job *)model{
    _model = model;
    if (self.title.length > 0) {
        self.title = self.title;
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}
- (UIButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(16, 0, 30, 30)];
        [_rightBtn setTitleColor:[@"#3E6F51" color] forState:UIControlStateNormal];
        self.input.rightViewMode = UITextFieldViewModeAlways;
        self.input.rightView = _rightBtn;
    }
    return  _rightBtn;
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidChangeSelection:(UITextField *)textField{
    if ([self.title isEqualToString:@"植物"]) {
        self.model.plant = textField.text;
    }else if ([self.title isEqualToString:@"数量"]){
        self.model.num = textField.text;
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([self.title isEqualToString:@"数量"] && ![string isEqualToString:@""] && ![string isNumber]) {
        return NO;
    }
    return YES;
}
@end
